import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

// 混淆函数库
import mixin from '../mixin/mixin.js'

// 时间格式化
import timeFormat from '../function/timeFormat.js'
// 时间戳格式化,返回多久之前
import timeFrom from '../function/timeFrom.js'

// 生成全局唯一guid字符串
import guid from '../function/guid.js'
// 延迟函数
import sleep from '../function/sleep.js'
// 打乱数组的顺序
import randomArray from '../function/randomArray.js'
// 对象和数组的深度克隆
import deepClone from '../function/deepClone.js'
// 对象深度拷贝
import deepMerge from '../function/deepMerge.js'


// 规则检验
import test from '../function/test.js'
// 随机数
import random from '../function/random.js'
// 去除空格
import trim from '../function/trim.js'
// toast提示，对uni.showToast的封装
import toast from '../function/toast.js'
// 获取父组件参数
import getParent from '../function/getParent.js'
// -----自定获取组件上的属性。
import getParentAttr from '../function/getParentAttr.js'
// -----自定获取单个父组件
import getParentAls from '../function/getParentAls.js'
// 上传文件库
import upload from '../function/upload.js'
// 预览文件
import preview from '../function/preview.js'

// obj转字样式style符串。  // -----自定
import {objToString} from '../function/objToString.js'
// 日历库
import calendar from '../function/calendar.js'
// canvas操作类。
import canvas from '../function/canvas.js'
import HandwritingSelf from '../function/signBoard.js'
//抽奖模型。
import choujiang from '../function/choujianggailv.js'
//json搜索器。
import * as jsSearch  from '../function/js-search.min.js'

import theme from '../function/theme.js'  
import request from '../request/index'  
import {$TestColor,setVueTiflyThemeBlac,$TestUnit}  from '../function/mixin.js'  


const $tm = {
	sleep,
	request,
	objToString,
	timeFormat: timeFormat,
	date: timeFormat, // 
	timeFrom,
	guid,
	upload,
	preview,
	randomArray,
	test,
	random,
	deepClone,
	deepMerge,
	getParent,
	getParentAttr,
	getParentAls,
	trim,
	toast,
	
	calendar,
	canvas,
	HandwritingSelf,
	choujiang,

	theme,
	jsSearch,
	setVueTiflyThemeBlac,
	$TestColor,
	$TestUnit
}



let moduleTrue={};
// 为了兼容如果用户，不按规范创建，或者不使用vuex时就可略过导入用户的模块。
try{
	const modulesList = require.context('@/store', true, /\.js$/);
	
	if(typeof modulesList === 'function' && typeof modulesList !=='undefined'){
		// 加载modules目录下所有文件(分模块)
		const modules = modulesList.keys().reduce((modules, modulePath) => {
			const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
			const value = modulesList(modulePath)
			modules[moduleName] = {namespaced: true,...value.default}
		
			return modules
		
		}, {});
		moduleTrue = modules;
	}
	
}catch(e){
	//TODO handle the exception
	console.warn('tmui提醒：用户未使用vuex')
}
let pdefault_cookies_color = uni.getStorageSync('setTmVuetifyColor')
let pdefault_cookies_black= uni.getStorageSync('setTmVuetifyBlack')



const store = new Vuex.Store({  
	modules:{
		...moduleTrue
	},
    state: {  
       tmVuetify: {
			color:  typeof pdefault_cookies_color === 'string'?pdefault_cookies_color:'',
			black: typeof pdefault_cookies_black === 'boolean'?pdefault_cookies_black:false,
			tmVueTifly_pages:'',
			tmVueTifly_pagesIndex:'',
       },
    }, 
	getters:{
		// $tm:state=>{
		// 	return $tm;
		// }
	},
    mutations: {  
       setTmVuetifyColor(state,color){
			Vue.set(state.tmVuetify,'color',color)
	   },
	   setPageNow(state,url){
		   Vue.set(state.tmVuetify,'tmVueTifly_pages',url);
	   },
	   setPageNowIndex(state,index){
	   		   Vue.set(state.tmVuetify,'tmVueTifly_pagesIndex',index);
	   },
	   setTmVuetifyBlack(state,black){
			Vue.set(state.tmVuetify,'black',black)
			if(black===true){
				uni.setTabBarStyle({
					backgroundColor:"#212121"
				})
			}else{
				uni.setTabBarStyle({
					backgroundColor:"#FFFFFF"
				})
			}
	   }
    }  
})

export default store;